package com.opensource.component.register;

import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ZonLen since on 2022/5/30 下午8:58
 */
@Slf4j
public final class RegisterStatementManager {

  private RegisterStatementManager() {
  }

  private static final Map<String, RegisterInterfaceStatement> ClIENT_REGISTER_INTERFACE_STATEMENTS = new HashMap<>();
  private static final Map<String, RegisterInterfaceStatement> SERVER_REGISTER_INTERFACE_STATEMENTS = new HashMap<>();

  protected static void init(RegisterInterfaceStatement registerInterfaceStatement) {
    log.info("【RegisterInterfaceStatement】 {}",
        JSON.toJSONString(registerInterfaceStatement));
    if (ClIENT_REGISTER_INTERFACE_STATEMENTS.containsKey(registerInterfaceStatement.getCode())) {
      throw new RuntimeException(registerInterfaceStatement.getCode() + " is existed");
    }
    ClIENT_REGISTER_INTERFACE_STATEMENTS
        .put(registerInterfaceStatement.getCode(), registerInterfaceStatement);
  }

  public static Map<String, RegisterInterfaceStatement> clientRegisterInterfaceStatement() {
    return ClIENT_REGISTER_INTERFACE_STATEMENTS;
  }

  public static String callbackUrl(String code) {
    return SERVER_REGISTER_INTERFACE_STATEMENTS.get(code).getCallbackUrl();
  }

  public static void register(RegisterInterfaceStatement registerInterfaceStatement) {
    SERVER_REGISTER_INTERFACE_STATEMENTS
        .put(registerInterfaceStatement.getCode(), registerInterfaceStatement);
  }

}
